#include "netdealwith.h"
#include<QDebug>
#include"sourcemanage.h"
#include "myhelper.h"
#include "algorithm.h"

NetDealwith::NetDealwith(NetSendReceive *pNetSendReceive,QObject *parent) :
    QObject(parent)
{
    qRegisterMetaType<NetDataPacket>("WorkStep");
    qRegisterMetaType<NetDataPacket>("ExpRawData");

    if(pNetSendReceive==NULL)
    {
        this->pNetSendReceive=new NetSendReceive(this);
    }
    else
    {
        this->pNetSendReceive=pNetSendReceive;
    }
    expType=0;
    workSubState=0;
    currentIndex=0;

    connect(pNetSendReceive,SIGNAL(SIG_ReceiveData(NetDataPacket)),this,SLOT(Decode(NetDataPacket)));
    //connect(this,SIGNAL(SIG_ResponseTimeout(uchar,uchar)),this,SLOT(OnStop()));

    pTimerWork=new QTimer(this);
    connect(pTimerWork,SIGNAL(timeout()),this,SLOT(OnWorkTimeout()));
    pTimerRead=new QTimer(this);
    connect(pTimerRead,SIGNAL(timeout()),this,SLOT(OnReadTimeout()));
    //pTimerRead->start(500);

    pTimerMonitor=new QTimer(this);
    connect(pTimerMonitor,SIGNAL(timeout()),this,SLOT(OnMonitorTimerOut()));
    pTimerMonitor->start(100);
}

void NetDealwith::StartStop(bool state,int type)
{
    if (state==true)
    {
        expType=type;
        SystemState systemState=SM->GetSystemState();
        systemState.START_STOP=1;
        SM->SetSystemState(systemState);

        pTimerWork->start(500);//
    }
    else
    {
        OnStop();
    }
}
void NetDealwith::OnStop()
{
    pTimerWork->stop();

    SetWaitIn(0);
    SystemState systemState=SM->GetSystemState();
    systemState.START_STOP=0;
    SM->SetSystemState(systemState);
}

void NetDealwith::OnWorkTimeout()
{
    switch(expType)
    {
        case 0:
            ProcessStep(currentIndex);
            break;
        case 1:
            ProcessExp(expIndex,sampleIndex,preIndex,mainIndex);
            break;
        case 2:
            {
                 QList<ExpRawData> rawDatas;
                 for(int i=0;i<6;i++)
                 {
                    ExpRawData expRawData;
                    Algorithm::InitRand();
                    expRawData.analogValue=Algorithm::Rand(1,1000);
                    expRawData.plusedValue=Algorithm::Rand(1,1000);
                    rawDatas.append(expRawData);
                 }
                 emit SIG_ReadResult(rawDatas);
            }
            break;
        case 4:
            OnStop();
            //StartStop(false);
            break;
    }
}
void NetDealwith::OnReadTimeout()
{
    //读取开关状态，各状态值
    ReadParas();
}
void NetDealwith::SetWaitIn(int value)
{
    waitIn=value;
    //qDebug()<<"waitIn"<<waitIn;
}

//exp
void NetDealwith::SetMessage(QString message)
{
    emit SIG_ExpStateChanged(message);
}

bool NetDealwith::StartExp()
{
    //清扫描中状态
    expSubStep=0;
    expIndex=0;
    sampleIndex=0;
    preIndex=0;
    mainIndex=0;

    StartStop(true,1);
    return true;
}

void NetDealwith::ProcessExp(int &expIndex,int &sampleIndex,int &preIndex,int &mainIndex)
{
    //1:获取实验 ，无实验执行15
    //2:进行实验前等待。。。
    //3:获取样本，无样本执行10
    //4:等待装置放入样本中。。。（等待或不等待，外界传入状态）
    //5:等待稳定时间
    //6:预扫描n
    //7:主扫描n
    //8:等待装置放入冲洗液中。。。（等待或不等待，外界传入状态）
    //9:等待稳定时间，时间到执行3
    //10:实验后等待。。。，时间到执行1
    //11:wait（外界传入状态）
    //14:超时
    //15:结束

    switch(expSubStep)
    {
        case EXP_GET_EXP:
            {
                QueueExp *pQueueExp=SM->GetQueueExp();
                QStringList lists=pQueueExp->GetFilePaths();
                for(;expIndex<lists.count();)//未完成实验
                {
                    filePath=lists.at(expIndex);
                    pExperiment=pQueueExp->GetExpPtr(filePath);

                    if(pExperiment->IsFinished()==2)
                    {
                        expIndex++;
                        continue;
                    }

                    QFileInfo fileInfo(filePath);
                    expName=fileInfo.fileName();
                    SetMessage("开始实验："+expName);
                    expSubStep=EXP_WAIT_BEFOR;
                    holdTime.start();
                    SetMessage("实验前等待...");
                    return;
                }
            }
            expSubStep=EXP_FINISH;
            break;
        case EXP_WAIT_BEFOR:
            {
                ExtractionTime *pExtractionTime=pExperiment->GetExtractionTimePtr();
                if(holdTime.elapsed()>1000*pExtractionTime->expBefore)
                {
                   expSubStep=EXP_GET_SAMPLE;
                }
            }
            break;
        case EXP_GET_SAMPLE://未完成样本
            {
                pExpSample=pExperiment->GetExpSamplePtr();
                QList<SampleInfor> sampleInfors=pExpSample->GetSampleInfors();
                for(sampleIndex=0;sampleIndex<sampleInfors.count();)//未完成样本(是否从0开始，可能删除或增加样本)
                {
                    pSampleInfor=pExpSample->GetSampleInforPtr(sampleIndex);
                    //
                    if(pSampleInfor->scanState==2)
                    {
                        sampleIndex++;
                        continue;
                    }
                    preIndex=0;
                    mainIndex=0;
                    SetMessage(QString("等待装置放入样本:%1").arg(pSampleInfor->sampleName));
                    expSubStep=EXP_WAIT_SAMPLE_IN;
                    return;
                }
                expSubStep=EXP_WAIT_AFTER;
                SetMessage("实验后等待...");
            }
            break;
        case EXP_WAIT_SAMPLE_IN:
            if(waitIn!=2)
            {
                emit SIG_WaitIn(1);//wait set
            }
            if(waitIn==0)
            {
                expSubStep=EXP_FINISH;
                break;
            }
            pSampleInfor->scanState=1;
            emit SIG_ExpScanChanged( filePath, sampleIndex, preIndex, mainIndex);
            holdTime.start();
            SetMessage(QString("等待稳定..."));
            expSubStep=EXP_WAIT_STABLE1;
            break;
        case EXP_WAIT_STABLE1:
            {
                ExtractionTime *pExtractionTime=pExperiment->GetExtractionTimePtr();
                if(holdTime.elapsed()>1000*pExtractionTime->extractStabilityTime)
                {
                   expSubStep=EXP_SCAN_PRE;
                }
            }
            break;
        case EXP_SCAN_PRE:
            if(preIndex<pSampleInfor->preScanCount)
            {
                //发送预扫描
//                SetHopScanParas(QList<ScanPara>,0,0,0,0,0);
//                SetSweepScanParas(QList<ScanPara>,0,0,0,0,0);
//                StartScan(1);
                pPreScanResults=pSampleInfor->GetPreScanResultPtr(preIndex);
                pPreScanResults->SetPreScanState(1);//状态改变，界面更新，防止编辑
                emit SIG_ExpScanChanged( filePath, sampleIndex, preIndex, mainIndex);
                SetMessage(QString("[%1]/[%2]:预扫描[%3]").arg(expName).arg(pSampleInfor->sampleName).arg(preIndex+1));
                expSubStep=EXP_WAIT_PRE;
                break;
            }
            expSubStep=EXP_SCAN_MAIN;
            break;
        case EXP_SCAN_MAIN:
            if(mainIndex<pSampleInfor->mainScanCount)
            {
                //发送主扫描
//                SetHopScanParas(QList<ScanPara>,0,0,0,0,0);
//                SetSweepScanParas(QList<ScanPara>,0,0,0,0,0);
//                StartScan(1);
                pMainScanResults=pSampleInfor->GetMainScanResultPtr(mainIndex);
                pMainScanResults->SetMainScanState(1);
                emit SIG_ExpScanChanged( filePath, sampleIndex, preIndex, mainIndex);
                SetMessage(QString("[%1]/[%2]:主扫描[%3]").arg(expName).arg(pSampleInfor->sampleName).arg(mainIndex+1));
                expSubStep=EXP_WAIT_MAIN;
                break;
            }
            pSampleInfor->scanState=2;
            emit SIG_ExpScanChanged( filePath, sampleIndex, preIndex, mainIndex);
            SetMessage(QString("等待装置放入清洗液中"));
            expSubStep=EXP_WAIT_WASH_IN;
            break;
        case EXP_WAIT_WASH_IN:
            if(waitIn!=2)
            {
                emit SIG_WaitIn(2);//wait set
            }
            if(waitIn==0)
            {
                expSubStep=EXP_FINISH;
                break;
            }
            SetMessage(QString("等待稳定..."));
            holdTime.start();
            expSubStep=EXP_WAIT_STABLE2;
            break;
        case EXP_WAIT_STABLE2:
            {
                ExtractionTime *pExtractionTime=pExperiment->GetExtractionTimePtr();
                if(holdTime.elapsed()>1000*pExtractionTime->washStabilityTime)
                {
                   sampleIndex++;
                   expSubStep=EXP_GET_SAMPLE;
                }
            }
            break;
        case EXP_WAIT_AFTER:
            {
                ExtractionTime *pExtractionTime=pExperiment->GetExtractionTimePtr();
                if(holdTime.elapsed()>1000*pExtractionTime->expAfter)
                {
                   expIndex++;
                   expSubStep=EXP_GET_EXP;
                }
            }
            break;
        case EXP_WAIT_PRE:
            //读取结果
            {
                //ReadScanResult();
                QList<ScanPara> scanParas;
                pExperiment->GetScanPara(scanParas,0);
                int count=0;
                foreach(ScanPara scanPara,scanParas)
                {
                    count+=scanPara.channels;
                }
                QList<ExpRawData> rawDatas;
                for(int i=0;i<count;i++)
                {
                   ExpRawData expRawData;
                   //Algorithm::InitRand();
                   expRawData.analogValue=Algorithm::Rand(1,1000);//Rand(0,1000000000)
                   expRawData.plusedValue=Algorithm::Rand(1,1000);
                   rawDatas.append(expRawData);
                }
                //emit SIG_ReadResult(rawDatas);
                FM->SaveToFile(filePath,pExperiment);
                pPreScanResults->SetPreExpRawData(rawDatas);
                pPreScanResults->SetPreScanState(2);
                emit SIG_ExpScanChanged( filePath, sampleIndex, preIndex, mainIndex);
                preIndex++;
                expSubStep=EXP_SCAN_PRE;
            }
            break;
        case EXP_WAIT_MAIN:
            //读取结果
            {
                //ReadScanResult();
                QList<ScanPara> scanParas;
                pExperiment->GetScanPara(scanParas,1);
                int count=0;
                foreach(ScanPara scanPara,scanParas)
                {
                    count+=scanPara.channels;
                }
                QList<ExpRawData> rawDatas;
                for(int i=0;i<count;i++)
                {
                   ExpRawData expRawData;
                   //Algorithm::InitRand();
                   expRawData.analogValue=Algorithm::Rand(1,1000);
                   expRawData.plusedValue=Algorithm::Rand(1,1000);
                   rawDatas.append(expRawData);
                }
                //emit SIG_ReadResult(rawDatas);
                FM->SaveToFile(filePath,pExperiment);
                pMainScanResults->SetMainExpRawData(rawDatas);
                pMainScanResults->SetMainScanState(2);
                emit SIG_ExpScanChanged( filePath, sampleIndex, preIndex, mainIndex);
                mainIndex++;
                expSubStep=EXP_SCAN_MAIN;
            }
            break;
        case EXP_TIMEOUT:
            OnStop();
            message=QString("当前状态：通讯超时");
            expSubStep=EXP_HOLD;
            return;
            break;
        case EXP_FINISH:
            OnStop();
            emit SIG_ExpFinish(1);
            expSubStep=EXP_HOLD;
            break;
        case EXP_HOLD:

            break;
    }
    //qDebug()<<"state"<<expSubStep;
}

//process
bool NetDealwith::ProcessWorkFlow(QString name)
{
    WorkFlow workFlow=SM->GetWorkFlow();
    if(workFlow.flows.count()==0) return false;
    if(workFlow.flows.contains(name)==false) return false;

    workName=name;
    workSteps=workFlow.flows.value(name);

    workSubState=0;
    currentIndex=0;
    StartStop(true);
    return true;
}
bool NetDealwith::ProcessStep(int &index)
{
    QString message;
    int count=workSteps.count();

    if(count<index+1)//完成
    {
        StartStop(false);
        SystemState state=SM->GetSystemState();
        if(workName=="打开真空")
        {
           state.READY=1;
        }
        else if(workName=="关闭真空")
        {
           state.READY=0;
        }
        else if(workName=="打开等离子体")
        {
            state.READY=2;
        }
        else if(workName=="关闭等离子体")
        {
            state.READY=1;
        }
        SM->SetSystemState(state);
        emit SIG_WorkFlowFinished(1);//workSteps.at(count-1).instrumentState;
        return true;
    }

    WorkStep workStep=workSteps.at(index);
    switch(workSubState)
    {
        case 0://0：提示状态，设置数值、开关
            message=QString("当前状态：%1").arg(workStep.runningInfor);
            emit SIG_StateChanged(message);
            SetEnvironmentParas1(workStep);
            SetEnvironmentParas2(workStep);
            SetLenBoardParas(workStep);
            SetDetectorParas(workStep);
            workSubState=1;
            holdTime.start();
            break;
        case 1://1：等待时间T1
            if(holdTime.elapsed()>1000*workStep.runningDelay)
            {
               workSubState=2;
            }
            break;
        case 2://2：开始超时计时T
            holdTime.start();
            workSubState=3;
            break;
        case 3://3：读取状态、数值、开关（或另开定时器处理），条件检测判断，条件满足5，超时4
            if(holdTime.elapsed()>1000*workStep.timeOut)
            {
               workSubState=4;
            }
            else
            {
                if(CheckAllCondition(workStep.conditions))//
                //if(1)//state
                {
                    workSubState=5;
                }
            }
            break;
        case 4://4：执行异常处理 工作流程
            {
                QString name;//读取异常流程名
                if(workName==name) //相同流程退出：防止死锁
                {
                    workSubState=9;
                }
                else
                {
                    bool state=ProcessWorkFlow(name);
                    if(state==false)
                    {
                        workSubState=9;
                    }
                    else
                    {
                        return true;
                       //workSubState=0;
                    }
                }
            }
            break;
        case 5://5：提示状态
            message=QString("当前状态：%1").arg(workStep.finishedInfor);
            emit SIG_StateChanged(message);
            workSubState=6;
            holdTime.start();
            break;
        case 6://6：等待时间T2，计时到再执行0-6，至步骤结束
            if(holdTime.elapsed()>1000*workStep.finishedDelay)
            {
               index++;
               workSubState=0;
            }
            break;

        case 9://其他异常退出
            StartStop(false);
            message=QString("当前状态：异常退出");
            emit SIG_StateChanged(message);
            emit SIG_WorkFlowFinished(0);

            workSubState=11;
            return false;
            break;
        case 10://通讯超时
            StartStop(false);
            message=QString("当前状态：通讯超时");
            emit SIG_StateChanged(message);
            emit SIG_WorkFlowFinished(0);

            workSubState=11;
            return false;
            break;
        case 11://hold
            break;
    }

    //0：提示状态，设置数值、开关
    //1：等待时间T1
    //2：开始超时计时T
    //3：读取状态、数值、开关（或另开定时器处理），条件检测判断，条件满足5，超时4
    //4：执行异常处理 工作流程（退出不执行异常处理：防止死锁）
    //5：提示状态
    //6：等待时间T2，计时到再执行0-6，至步骤结束
}

bool NetDealwith::OpenVacuum()
{

}
bool NetDealwith::CloseVacuum()
{

}
bool NetDealwith::OpenPlasma()
{

}
bool NetDealwith::ClosePlasma()
{

}

bool NetDealwith::CheckAllCondition(QList<WorkStepCondition>  conditions)
{
    if(conditions.count()<7) return false;
    int i;
    bool state;

    i=0;
    if(conditions.at(i).bIsChoose==true)
    {
        state=CheckCondition(workStepRead.turboPumpSpeed,conditions.at(i).index,conditions.at(i).value);
        if(state==false) return false;
    }
    i=1;
    if(conditions.at(i).bIsChoose==true)
    {
        state=CheckCondition(workStepRead.turboPumpCurrent,conditions.at(i).index,conditions.at(i).value);
        if(state==false) return false;
    }
    i=2;
    if(conditions.at(i).bIsChoose==true)
    {
        state=CheckCondition(workStepRead.conePress,conditions.at(i).index,conditions.at(i).value);
        if(state==false) return false;
    }
    i=3;
    if(conditions.at(i).bIsChoose==true)
    {
        state=CheckCondition(workStepRead.chamberPress,conditions.at(i).index,conditions.at(i).value);
        if(state==false) return false;
    }
    i=4;
    if(conditions.at(i).bIsChoose==true)
    {
        state=CheckCondition(workStepRead.coolwarter,conditions.at(i).index,conditions.at(i).value);
        if(state==false) return false;
    }
    i=5;
    if(conditions.at(i).bIsChoose==true)
    {
        state=CheckCondition(workStepRead.samplingConeT,conditions.at(i).index,conditions.at(i).value);
        if(state==false) return false;
    }
    i=6;
    if(conditions.at(i).bIsChoose==true)
    {
        state=CheckCondition(workStepRead.atomizationT,conditions.at(i).index,conditions.at(i).value);
        if(state==false) return false;
    }

    return true;
}

bool NetDealwith::CheckCondition(double value,int index,double setValue)
{
    switch(index)
    {
        case 0://>
            if(value>setValue) return true;
            else return false;
        break;
        case 1://<
            if(value<setValue) return true;
            else return false;
        break;
        case 2://=
            if(value=setValue) return true;
            else return false;
        break;
        case 3://>=
            if(value>=setValue) return true;
            else return false;
        break;
        case 4://<=
            if(value<=setValue) return true;
            else return false;
        break;
    }
    return true;
}

bool NetDealwith::StartTune(bool state)
{
    //清扫描中状态
    expType=2;
    StartStop(state,2);
    return true;
}

//cmd-new
bool NetDealwith::ReadSerialNum()
{
   NetDataPacket sendData;
   NetDataPacket receiveData;

   sendData.cmd1=0x20;
   sendData.cmd2=0x55;
   sendData.length=0;
   sendData.data.resize(sendData.length);
   bool state=pNetSendReceive->SendData(sendData,receiveData);
   if(state>0)
   {
       ResendAdd(sendData);
   }
   return state;
}

bool NetDealwith::SetSerialNum(QString num)//12/13byte
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x20;
    sendData.cmd2=0x66;
    sendData.length=12;
    sendData.data.resize(sendData.length);
    QByteArray data;
    data=num.toLatin1();//toAscii()
    for(int i=0;i<data.size();i++)
    {
        sendData.data[i]=data.at(i);
    }
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        ResendAdd(sendData);
    }
    return state;

}
bool NetDealwith::ReadSoftwareVersion(uchar type)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x21;
    sendData.cmd2=0x55;
    sendData.length=1;
    sendData.data.resize(sendData.length);
    sendData.data[0]=type;
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        ResendAdd(sendData);
    }
    return state;
}
bool NetDealwith::ReadEnvironmentParas()
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x24;
    sendData.cmd2=0x55;
    sendData.length=0;
    sendData.data.resize(sendData.length);
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        //ResendAdd(sendData);
    }
    return state;
}
bool NetDealwith::SetEnvironmentParas1(WorkStep workStep)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;
    int index=0;

    sendData.cmd1=0x24;
    sendData.cmd2=0x66;
    sendData.length=4;//
    sendData.data.resize(sendData.length);
    EncodeInt(workStep.switchValue.value,sendData.data,index);

    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        ResendAdd(sendData);
    }
    return state;
}

bool NetDealwith::SetEnvironmentParas2(WorkStep workStep)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;
    int index=0;

    sendData.cmd1=0x24;
    sendData.cmd2=0x67;
    sendData.length=45;//
    sendData.data.resize(sendData.length);
    sendData.data[0]=workStep.peristalticPumpSpeed;//
    index+=1;
    EncodeFloat(workStep.motorX,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.motorY,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.motorZ,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.atomizationT,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.atomization,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.auxiliary,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.cooler,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.collisionGasHe,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.collisionGasH2,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.added,sendData.data,index);
    index+=4;
    //EncodeFloat(workStep.added,sendData.data,index);//

    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        ResendAdd(sendData);
    }
    return state;
}

bool NetDealwith::ReadLenBoardParas()
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x25;
    sendData.cmd2=0x55;
    sendData.length=0;
    sendData.data.resize(sendData.length);
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        //ResendAdd(sendData);
    }
    return state;
}

bool NetDealwith::SetLenBoardParas(WorkStep workStep)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;
    int index=0;

    sendData.cmd1=0x25;
    sendData.cmd2=0x66;
    sendData.length=44;//
    sendData.data.resize(sendData.length);
    EncodeFloat(workStep.extractionLen,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.quadrupole,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.hexapole,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.len1,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.len2,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.len3,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.focusingLen,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.deflectionLen1,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.deflectionLen2,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.deflectionLen3,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.deflectionLen4,sendData.data,index);
    index+=4;

    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        ResendAdd(sendData);
    }
    return state;
}
bool NetDealwith::ReadAlarm()
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x26;
    sendData.cmd2=0x55;
    sendData.length=0;
    sendData.data.resize(sendData.length);
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        ResendAdd(sendData);
    }
    return state;
}
bool NetDealwith::SetAlarm(uchar length,QByteArray data)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x26;
    sendData.cmd2=0x66;
    sendData.length=1+length;//
    sendData.data.resize(sendData.length);
    sendData.data[0]=length;
    data.resize(length);
    sendData.data.append(data);
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        ResendAdd(sendData);
    }
    return state;
}
bool NetDealwith::ReadDetectorParas()
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x27;
    sendData.cmd2=0x55;
    sendData.length=0;
    sendData.data.resize(sendData.length);
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        //ResendAdd(sendData);
    }
    return state;
}

bool NetDealwith::SetDetectorParas(WorkStep workStep)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;
    int index=0;

    sendData.cmd1=0x27;
    sendData.cmd2=0x66;
    sendData.length=21;//
    sendData.data.resize(sendData.length);
    EncodeShort(workStep.analogDetector,sendData.data,index);
    index+=2;
    EncodeShort(workStep.digitalDetector,sendData.data,index);
    index+=2;
    EncodeFloat(workStep.screeningV,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.analogOffsetV,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.gateOpen,sendData.data,index);
    index+=4;
    EncodeFloat(workStep.gateClose,sendData.data,index);
    index+=4;
    sendData.data[index]=0;//1Byte 分辨标志位：

    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        ResendAdd(sendData);
    }
    return state;
}

bool NetDealwith::SetLimits(LimitValue limitValue)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;
    int index=0;

    sendData.cmd1=0x31;
    sendData.cmd2=0x66;
    sendData.length=64;//
    sendData.data.resize(sendData.length);
    EncodeFloat(limitValue.coolwarterUp,sendData.data,index);
    index+=4;
    EncodeFloat(limitValue.coolwarterLow,sendData.data,index);
    index+=4;
    EncodeFloat(limitValue.exhaustUp,sendData.data,index);
    index+=4;
    EncodeFloat(limitValue.exhaustLow,sendData.data,index);
    index+=4;
    EncodeFloat(limitValue.interTempUp,sendData.data,index);
    index+=4;
    EncodeFloat(limitValue.InterTempLow,sendData.data,index);
    index+=4;
    EncodeFloat(limitValue.rfPowerUp,sendData.data,index);
    index+=4;
    EncodeFloat(limitValue.rfPowerLow,sendData.data,index);
    index+=4;
    EncodeFloat(limitValue.atomizationUp,sendData.data,index);
    index+=4;
    EncodeFloat(limitValue.atomizationLow,sendData.data,index);
    index+=4;
    EncodeFloat(limitValue.auxiliaryUp,sendData.data,index);
    index+=4;
    EncodeFloat(limitValue.auxiliaryLow,sendData.data,index);
    index+=4;
    EncodeFloat(limitValue.coolerUp,sendData.data,index);
    index+=4;
    EncodeFloat(limitValue.coolerLow,sendData.data,index);
    index+=4;
    EncodeFloat(limitValue.referVoltUp,sendData.data,index);
    index+=4;
    EncodeFloat(limitValue.referVoltLow,sendData.data,index);

    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        ResendAdd(sendData);
    }
    return state;
}
bool NetDealwith::SetPoleCalRatio(CalRatio four,CalRatio six)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;
    int index=0;
    float test1=1.234;
    int test2=1234;

    sendData.cmd1=0x32;
    sendData.cmd2=0x66;
    sendData.length=40;//
    sendData.data.resize(sendData.length);
    EncodeFloat(test1,sendData.data,index);//four.ratios.at(0)
    index+=4;
    EncodeFloat(test1,sendData.data,index);
    index+=4;
    //EncodeFloat(four.ratios.at(2),sendData.data,index);
    EncodeInt(test2,sendData.data,index);
    index+=4;
    EncodeFloat(four.ratios.at(3),sendData.data,index);
    index+=4;
    EncodeFloat(four.ratios.at(4),sendData.data,index);
    index+=4;
    EncodeFloat(six.ratios.at(0),sendData.data,index);
    index+=4;
    EncodeFloat(six.ratios.at(1),sendData.data,index);
    index+=4;
    EncodeFloat(six.ratios.at(2),sendData.data,index);
    index+=4;
    EncodeFloat(six.ratios.at(3),sendData.data,index);
    index+=4;
    EncodeFloat(six.ratios.at(4),sendData.data,index);
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        ResendAdd(sendData);
    }
    return state;
}
bool NetDealwith::SetSecurityAmu(float amu)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x33;
    sendData.cmd2=0x66;
    sendData.length=4;//
    sendData.data.resize(sendData.length);
    EncodeFloat(amu,sendData.data,0);
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        ResendAdd(sendData);
    }
    return state;
}
bool NetDealwith::SetBuildTimeCalRatio(double k,double b)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;
    int index=0;

    sendData.cmd1=0x34;
    sendData.cmd2=0x66;
    sendData.length=8;//
    sendData.data.resize(sendData.length);
    EncodeFloat(k,sendData.data,index);//
    index+=4;
    EncodeFloat(b,sendData.data,index);//
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        ResendAdd(sendData);
    }
    return state;
}
bool NetDealwith::StartScan(uchar type)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x40;
    sendData.cmd2=0x66;
    sendData.length=1;//
    sendData.data.resize(sendData.length);
    sendData.data[0]=type;
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        ResendAdd(sendData);
    }
    return state;
}
bool NetDealwith::SetHopScanParas(QList<ScanPara> paras,double value1,double value2,double value3,double value4,short value5)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;
    int index=0;
    uchar num=paras.count();

    sendData.cmd1=0x41;
    sendData.cmd2=0x66;
    sendData.length=14*num+14;//9*num+14
    sendData.data.resize(sendData.length);
    for(int i=0;i<num;i++)
    {
        EncodeFloat(paras.at(i).startMassNum,sendData.data,index);//
        index+=4;
        EncodeFloat(paras.at(i).endMassNum,sendData.data,index);//
        index+=4;
        sendData.data[index]=paras.at(i).channels;
        index+=1;
        EncodeFloat(paras.at(i).dwelltime,sendData.data,index);//
        index+=4;
        sendData.data[index]=SM->options.value("ResolutionType").indexOf(paras.at(i).type);
        index+=1;
    }
//
    EncodeFloat(value1,sendData.data,index);
    index+=4;
    EncodeFloat(value2,sendData.data,index);
    index+=4;
    EncodeFloat(value3,sendData.data,index);
    index+=4;
    EncodeFloat(0,sendData.data,index);//
    index+=4;
    EncodeFloat(value4,sendData.data,index);
    index+=4;
    EncodeShort(value5,sendData.data,index);

    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        ResendAdd(sendData);
    }
    return state;
}

bool NetDealwith::SetSweepScanParas(QList<ScanPara> paras,double value1,double value2,double value3,double value4,short value5)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;
    int index=0;
    uchar num=paras.count();

    sendData.cmd1=0x41;
    sendData.cmd2=0x67;
    sendData.length=14*num+14;//22*n+14
    sendData.data.resize(sendData.length);
    //
    for(int i=0;i<num;i++)
    {
        EncodeFloat(paras.at(i).startMassNum,sendData.data,index);//
        index+=4;
        EncodeFloat(paras.at(i).endMassNum,sendData.data,index);//
        index+=4;
        sendData.data[index]=paras.at(i).channels;//paras.at(i).channelsAmu
        index+=1;
        EncodeFloat(paras.at(i).dwelltime,sendData.data,index);//
        index+=4;
        sendData.data[index]=SM->options.value("ResolutionType").indexOf(paras.at(i).type);
        index+=1;
    }
//
    EncodeFloat(value1,sendData.data,index);
    index+=4;
    EncodeFloat(value2,sendData.data,index);
    index+=4;
    EncodeFloat(value3,sendData.data,index);
    index+=4;
    EncodeFloat(0,sendData.data,index);//
    index+=4;
    EncodeFloat(value4,sendData.data,index);
    index+=4;
    EncodeShort(value5,sendData.data,index);

    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        ResendAdd(sendData);
    }
    return state;
}
bool NetDealwith::ReadScanResult()
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x42;
    sendData.cmd2=0x55;
    sendData.length=0;//
    sendData.data.resize(sendData.length);
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    if(state>0)
    {
        ResendAdd(sendData);
    }
    return state;
}

bool NetDealwith::SetParas()
{
    SetEnvironmentParas1(workStepSet);
    SetEnvironmentParas2(workStepSet);
    SetLenBoardParas(workStepSet);
    SetDetectorParas(workStepSet);
}
bool NetDealwith::ReadParas()
{
    ReadEnvironmentParas();
    ReadLenBoardParas();
    ReadDetectorParas();
}
//cmd-old
/*
bool NetDealwith::ReadSerialNum()
{
   NetDataPacket sendData;
   NetDataPacket receiveData;

   sendData.cmd1=0x20;
   sendData.cmd2=0x55;
   sendData.length=0;
   sendData.data.resize(sendData.length);
   bool state=pNetSendReceive->SendData(sendData,receiveData);
   if(state>0)
   {
       ResendAdd(sendData);
   }
   return state;
}

bool NetDealwith::SetSerialNum(QString num)//12/13byte
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x20;
    sendData.cmd2=0x66;
    sendData.length=12;
    sendData.data.resize(sendData.length);
    QByteArray data;
    data=num.toAscii();
    for(int i=0;i<data.size();i++)
    {
        sendData.data[i]=data.at(i);
    }
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    return state;

}
bool NetDealwith::ReadSoftwareVersion(uchar type)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x21;
    sendData.cmd2=0x55;
    sendData.length=1;
    sendData.data.resize(sendData.length);
    sendData.data[0]=type;
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //
    return state;
}

bool NetDealwith::ReadDetectorV(uchar type)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x22;
    sendData.cmd2=0x55;
    sendData.length=1;
    sendData.data.resize(sendData.length);
    sendData.data[0]=type;
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //
    return state;
}

bool NetDealwith::SetDetectorV(uchar type)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x22;
    sendData.cmd2=0x66;
    sendData.length=5;
    sendData.data.resize(sendData.length);
    sendData.data[0]=type;
    //
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //
    return state;
}

bool NetDealwith::ReadLimits(uchar type)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x23;
    sendData.cmd2=0x55;
    sendData.length=1;
    sendData.data.resize(sendData.length);
    sendData.data[0]=type;
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //
    return state;
}
bool NetDealwith::SetLimits(uchar type)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x23;
    sendData.cmd2=0x66;
    sendData.length=5;
    sendData.data.resize(sendData.length);
    sendData.data[0]=type;
    //
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //
    return state;
}

bool NetDealwith::ReadMotorParas()
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x24;
    sendData.cmd2=0x55;
    sendData.length=0;
    //sendData.data.resize(sendData.length);

    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //24
    return state;
}

bool NetDealwith::ReadSystemParas()
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x24;
    sendData.cmd2=0x55;
    sendData.length=0;
    //sendData.data.resize(sendData.length);

    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //n
    return state;
}

bool NetDealwith::ReadResolution(uchar type)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x35;
    sendData.cmd2=0x55;//
    sendData.length=1;
    sendData.data.resize(sendData.length);
    sendData.data[0]=type;

    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //5
    return state;
}
bool NetDealwith::SetResolution(uchar type)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x35;
    sendData.cmd2=0x66;//
    sendData.length=1;
    sendData.data.resize(sendData.length);
    sendData.data[0]=type;
    //4
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //1
    return state;
}

bool NetDealwith::ReadCal(uchar type)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x37;
    sendData.cmd2=0x55;//
    sendData.length=1;
    sendData.data.resize(sendData.length);
    sendData.data[0]=type;

    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //9
    return state;
}
bool NetDealwith::SetCal(uchar type)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x37;
    sendData.cmd2=0x66;//
    sendData.length=1;
    sendData.data.resize(sendData.length);
    sendData.data[0]=type;
    //8
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //1
    return state;
}

bool NetDealwith::SetAlarm(uchar length)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x3a;
    sendData.cmd2=0x66;//
    sendData.length=length+1;
    sendData.data.resize(sendData.length);
    sendData.data[0]=length;
    //length
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //1
    return state;
}

bool NetDealwith::ReadGroundFire(uchar ground,uchar fire)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x3b;
    sendData.cmd2=0x55;//
    sendData.length=0;
    //sendData.data.resize(sendData.length);

    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //2
    return state;
}
bool NetDealwith::SetGroundFire(uchar ground,uchar fire)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x3b;
    sendData.cmd2=0x66;//
    sendData.length=2;
    sendData.data.resize(sendData.length);
    sendData.data[0]=ground;
    sendData.data[1]=fire;
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //1
    return state;
}

bool NetDealwith::ReadScreeningVol(uchar type)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x3c;
    sendData.cmd2=0x55;//
    sendData.length=1;
    sendData.data.resize(sendData.length);
    sendData.data[0]=type;

    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //9
    return state;
}
bool NetDealwith::SetScreeningVol(uchar type)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x3c;
    sendData.cmd2=0x66;//
    sendData.length=9;
    sendData.data.resize(sendData.length);
    sendData.data[0]=type;
    //8
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //1
    return state;
}

bool NetDealwith::SetCollection(uchar type,uchar other)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x40;
    sendData.cmd2=0x66;//
    sendData.length=2;
    sendData.data.resize(sendData.length);
    sendData.data[0]=type;
    sendData.data[1]=other;

    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //1
    return state;
}

bool NetDealwith::ReadHopPara()
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x43;
    sendData.cmd2=0x55;//
    sendData.length=0;
    sendData.data.resize(sendData.length);

    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //214
    return state;
}
bool NetDealwith::SetHopPara()
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x43;
    sendData.cmd2=0x66;//
    sendData.length=214;
    sendData.data.resize(sendData.length);
    //214

    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //1
    return state;
}

bool NetDealwith::ReadData()
{

}
bool NetDealwith::SetMethod()
{

}
bool NetDealwith::SetFrequency()
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x48;
    sendData.cmd2=0x66;//
    sendData.length=8;
    sendData.data.resize(sendData.length);
    //8
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //1
    return state;
}
bool NetDealwith::SetHeterogeneous(uchar type)
{
    NetDataPacket sendData;
    NetDataPacket receiveData;

    sendData.cmd1=0x50;
    sendData.cmd2=0x66;//
    sendData.length=13;
    sendData.data.resize(sendData.length);
    sendData.data[0]=type;
    //12
    bool state=pNetSendReceive->SendData(sendData,receiveData);
    //1
    return state;
}
*/

//
void NetDealwith::Decode(NetDataPacket recvPacket)
{
    ResendDelete(recvPacket);
    switch(recvPacket.cmd1)
    {
        case 0x20://序列号
            if(recvPacket.cmd2==0x55)
            {
                //12
                QString name=recvPacket.data;
            }
            else
            {

            }
        break;
        case 0x21://版本
            if(recvPacket.cmd2==0x55)
            {
                QString version;
                uchar type=recvPacket.data.at(0);
                switch(type)
                {
                case 0://主板
                    //32
                    version=recvPacket.data.remove(0,1);
                    break;
                case 1://FPGA
                    version=recvPacket.data.remove(0,1);
                    break;
                case 2://环境板
                    version=recvPacket.data.remove(0,1);
                    break;
                case 3://透镜板
                    version=recvPacket.data.remove(0,1);
                    break;
                }
            }
            else
            {

            }
        break;
        case 0x24://环境板参数
            if(recvPacket.cmd2==0x55)
            {
                uchar temp=recvPacket.data[0];
                int index=0;
                if(temp==1)
                {

                }
                else if(temp==2)
                {
                    return;
                }
                else //
                {
                    return;
                }

                workStepRead.peristalticPumpSpeed=recvPacket.data[1];
                index=2;
                workStepRead.switchValue.value=DecodeInt(recvPacket.data.mid(index));
                index+=4;
                workStepRead.turboPumpSpeed=DecodeShort(recvPacket.data.mid(index));
                index+=2;
                workStepRead.turboPumpCurrent=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.turboPumpT=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.conePress=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.chamberPress=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.motorX=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.motorY=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.motorZ=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.directXR=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.directYR=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.inBoxT=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.inTorchT=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.samplingConeT=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                //reserve
                index+=4;
                workStepRead.atomizationT=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.coolwarter=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.exhaustSensor=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.atomizationP=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.atomization=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.auxiliary=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.cooler=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.collisionGasHe=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.collisionGasH2=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.added=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                //reserve
                index+=4;
                workStepRead.icpCurrent=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.icpT=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.icpPositive=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                //reserve
                //index+=4;
                emit SIG_ReadValue(workStepRead);
            }
            else
            {

            }
        break;
        case 0x25://透镜板参数
            if(recvPacket.cmd2==0x55)
            {
                uchar temp=recvPacket.data[0];
                int index=0;
                if(temp==1)
                {

                }
                else if(temp==2)
                {
                    return;
                }
                else //
                {
                    return;
                }

                workStepRead.extractionLen=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.quadrupole=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.hexapole=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.len1=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.len2=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.len3=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.focusingLen=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.deflectionLen1=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.deflectionLen2=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.deflectionLen3=DecodeFloat(recvPacket.data.mid(index));
                index+=4;
                workStepRead.deflectionLen4=DecodeFloat(recvPacket.data.mid(index));
                emit SIG_ReadValue(workStepRead);
            }
            else
            {

            }
        break;
        case 0x26://报警码
            if(recvPacket.cmd2==0x55)
            {

            }
            else
            {

            }
        break;
        case 0x27://检测器与分辨率
            if(recvPacket.cmd2==0x55)
            {
                int index=0;
                workStepRead.analogDetector=DecodeShort(recvPacket.data.mid(index));
                index+=2;
                workStepRead.digitalDetector=DecodeShort(recvPacket.data.mid(index));
                index+=2;
                emit SIG_ReadValue(workStepRead);
            }
            else
            {

            }
        break;
        case 0x31://报警码上下限
            if(recvPacket.cmd2==0x55)
            {

            }
            else
            {

            }
        break;
        case 0x32://标准曲线
            if(recvPacket.cmd2==0x55)
            {

            }
            else
            {
                //test
                float value;
                int test;
                value=DecodeFloat(recvPacket.data.mid(0,4));
                qDebug()<<QString::number(value,'f',3);
                test=DecodeInt(recvPacket.data.mid(4,4));
                qDebug()<<QString::number(test,'f',3);
                test=DecodeInt(recvPacket.data.mid(8,4));
                qDebug()<<QString::number(test,'f',3);
            }
        break;
        case 0x33://安全质量
            if(recvPacket.cmd2==0x55)
            {

            }
            else
            {

            }
        break;
        case 0x34://建立时间校准
            if(recvPacket.cmd2==0x55)
            {

            }
            else
            {

            }
        break;
        case 0x40://启停扫描
            if(recvPacket.cmd2==0x55)
            {

            }
            else
            {

            }
        break;
        case 0x41://扫描参数写
            if(recvPacket.cmd2==0x55)
            {

            }
            else
            {

            }
        break;
        case 0x42://扫描结果读
            if(recvPacket.cmd2==0x55)
            {
                int type=recvPacket.data.at(0);
                QByteArray data=recvPacket.data.mid(5);
                int count=data.count()/12;
                switch(type)
                {
                case 0://模拟数据
                    //break;
                case 1://脉冲计数数据
                    //break;
                case 2://有两种数据
                    expRawDatas.clear();
                    for(int i=0;i<count;i++)
                    {
                        ExpRawData rawData;
                        rawData.analogValue=DecodeFloat(data.mid(i*10,4));
                        rawData.plusedValue=DecodeInt(data.mid(i*10+4,4));
                        rawData.sweepCount=DecodeShort(data.mid(i*10+8,2));
                        expRawDatas.append(rawData);
                    }
                    emit SIG_ReadResult(expRawDatas);
                    break;
                case 3://数据没有准备好
                    break;
                case 4://下传的包号超限
                    break;
                }
            }
            else
            {

            }
        break;
    }
}

void NetDealwith::Encode(NetDataPacket recvPacket)
{

}

void NetDealwith::EncodeFloat(float value,QByteArray &data,int startIndex)
{
    uchar *p=(uchar *)&value;
    for(int i=0;i<4;i++)
    {
        data[i+startIndex]=p[i];
    }
}
float NetDealwith::DecodeFloat(QByteArray data)
{
    float value;
    uchar *p=(uchar *)&value;
    for(int i=0;i<4;i++)
    {
        p[i]=data.at(i);
    }
    return value;
}
void NetDealwith::EncodeInt(int value,QByteArray &data,int startIndex)//可参照 EncodeFloat
{
    uchar *p=(uchar *)&value;
    for(int i=0;i<4;i++)
    {
        data[i+startIndex]=p[i];
    }
}
int NetDealwith::DecodeInt(QByteArray data)
{
    int value;
    uchar *p=(uchar *)&value;
    for(int i=0;i<4;i++)
    {
        p[i]=data.at(i);
    }
    return value;
}
void NetDealwith::EncodeShort(short value,QByteArray &data,int startIndex)
{
    uchar *p=(uchar *)&value;
    for(int i=0;i<2;i++)
    {
        data[i+startIndex]=p[i];
    }
}
int NetDealwith::DecodeShort(QByteArray data)
{
    short value;
    uchar *p=(uchar *)&value;
    for(int i=0;i<2;i++)
    {
        p[i]=data.at(i);
    }
    return value;
}
//超时重发
#include<QThread>
void NetDealwith::ResendAdd(NetDataPacket sendPacket)
{
    sendState=0;
    QString key=QTime::currentTime().toString("hh:mm:ss.zzz");
    for(int i=0;i<20;i++)
    {
        hashMap[i].insert(key,sendPacket);
    }
    QThread::usleep(1000);
    //usleep(1000);//避免key相同
}
void NetDealwith::ResendDelete(NetDataPacket recvPacket)
{
    QString str;
    for(int i=0;i<20;i++)
    {
        foreach(str,hashMap[i].keys())
        {
            if(hashMap[i].value(str).cmd1==(recvPacket.cmd1)
                    &&hashMap[i].value(str).cmd2==(recvPacket.cmd2))
            {
                hashMap[i].remove(str);
                break;//避免删除后面的命令
            }
        }
    }
}
void NetDealwith::ResendDeleteAll()
{
    for(int i=0;i<20;i++)
    {
        hashMap[i].clear();
    }
}

void NetDealwith::OnMonitorTimerOut()
{
    QString str;
    NetDataPacket recvPacket;
    int toTime;
    for(int i=0;i<20;i++)
    {
        foreach(str,hashMap[i].keys())
        {
            toTime=QTime::currentTime().msecsTo(QTime::fromString(str,"hh:mm:ss.zzz"));
            if(toTime<-250*(i+1))  //
            {
                pNetSendReceive->SendData(hashMap[i].value(str),recvPacket);
                if(i==19)
                {
                    if(sendState==0)
                    {
                        sendState=1;
                        workSubState=10;
                        expSubStep=10;
                        emit SIG_ResponseTimeout(hashMap[i].value(str).cmd1,hashMap[i].value(str).cmd2);
                        ResendDeleteAll();
                    }
                }
                hashMap[i].remove(str);
            }
            else if(toTime>0)
            {
                if(toTime-86400000<-250*(i+1))
                {
                    pNetSendReceive->SendData(hashMap[i].value(str),recvPacket);
                    if(i==19)
                    {
                        if(sendState==0)
                        {
                            sendState=1;
                            workSubState=10;
                            expSubStep=10;
                            emit SIG_ResponseTimeout(hashMap[i].value(str).cmd1,hashMap[i].value(str).cmd2);
                            ResendDeleteAll();
                        }
                    }
                    hashMap[i].remove(str);
                }
            }
        }

    }
}
